<template>
  <div>
    <!-- 头部-面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>

      <el-table :data="rolesList" border stripe>
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row :class="['bdBottom', index ===0?'bdTop':'', 'vcenter']" v-for="(item, index) in scope.row.children" :key="item.id">
              <!-- 渲染一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="removeRightById()">{{item.authName}}
                </el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 渲染二、三级权限 -->
              <el-col :span="19">
                <el-row :class="[_index ===0?'':'bdTop', 'vcenter']" v-for="(_item, _index) in item.children" :key="_item.id">
                  <!-- 二级 -->
                  <el-col :span="6">
                    <el-tag closable type="success" @close="removeRightById()">{{_item.authName}}
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 三级 -->
                  <el-col :span="13">
                    <el-tag closable @close="removeRightById()" type="warning" v-for="__item in _item.children" :key="__item.id">{{__item.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="edit(scope.row)" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteItem(scope.row)" icon="el-icon-delete">删除</el-button>
            <el-button @click="showSetRightDialog(scope.row)" size="mini" type="warning" icon="el-icon-setting">权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

  <!-- 展示分配权限的对话框 -->
  <el-dialog title="分配权限" :visible.sync="setRightDialogShow" width="50%">
    <!-- 树形控件 -->
    <el-tree
      :data="allRightsDictList"
      :props="treeProps"
      show-checkbox
      node-key="id"
      default-expand-all
      ref="treeRef"
      :default-checked-keys="defKeys"/>
    <span>
      <el-button @click="closeDialog">取消</el-button>
      <el-button @click="confirmBtn" type="primary">确定</el-button>
    </span>
  </el-dialog>
  </div>
</template>

<script>
import { getRolesList, getAllrightsDict } from '@/utils/https.js'
export default {
  data() {
    return {
      rolesList: [],
      setRightDialogShow: false,
      allRightsDictList: [],
      treeProps: {
        children: 'children',
        label: 'authName'
      },
      defKeys: [] // 默认选择的节点
    }
  },
  created() {
    this.queryRolesList()
  },
  methods: {
    async queryRolesList() { // 获取角色列表
      const { data: res } = await getRolesList()
      if (res.data.status !== 200) {
        return this.$message.error('获取角色列表失败')
      }
      this.rolesList = res.data.list
      console.log(this.rolesList)
    },
    edit(rowData) {},
    deleteItem(rowData) {},
    async removeRightById() {
      const res = await this.$confirm('是否删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).catch(err => err)
      if (res !== 'confirm') {
        return this.$message.info('取消了删除')
      }
      // 重新找到那一个权限去赋值：好处是防止展开行关闭
    },
    async showSetRightDialog(row) { // 展示分配权限的对话框
      // 获取所有权限数据
      const { data: res } = await getAllrightsDict()
      this.allRightsDictList = res.data.list
      // 递归三级节点
      this.getLeafId(row, this.defKeys)
      this.setRightDialogShow = true
    },

    // 递归获取三级节点id
    getLeafId(node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(e => this.getLeafId(e, arr))
    },
    confirmBtn() {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      console.log(keys)
      this.$$message.success('分配权限成功')
      this.queryRolesList()
      this.setRightDialogShow = false
    },
    closeDialog() {
      this.setRightDialogShow = false
      this.defKeys = []
    }
  }
}
</script>

<style lang="less" scoped>
.el-tag{
  margin: 7px;
}
.bdTop {
  border-top: 1px solid #eee;
}
.bdBottom {
  border-bottom: 1px solid #eee;
}
.vcenter{
  display: flex;
  align-items: center;
}
</style>
